package com.example.demo.controller;

import com.example.demo.entity.Storage;
import com.example.demo.service.StorageService;
import com.example.demo.util.RestBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/storages")
public class StorageController {

    @Autowired
    private StorageService storageService;

    @GetMapping("/{sid}")
    public RestBean<Storage> getStorage(@PathVariable Integer sid) {
        Storage storage = storageService.findStorageById(sid);
        return storage != null ?
                RestBean.success(storage) :
                RestBean.failure(404, "仓库不存在");
    }

    @PostMapping
    public RestBean<Storage> createStorage(@RequestBody Storage storage) {
        int result = storageService.createStorage(storage);
        return result == 1 ?
                RestBean.success(storage, "创建成功") :
                RestBean.failure(400, "创建失败");
    }

    @GetMapping("/company/{cid}")
    public RestBean<List<Storage>> getByCompany(@PathVariable Integer cid) {
        return RestBean.success(storageService.findStoragesByCompanyId(cid));
    }
}